<template>
  <div class="container">
    <div class="login-form-wrapper">
      <el-tabs type="border-card" style="height: 400px; background-color: rgba(255, 255, 255, 0.5);">
        <el-tab-pane label="登录">
          <el-form class="login-form" :model="loginForm" @submit.native.prevent="handleLogin">
            <el-form-item label="用户名">
              <el-input v-model="loginForm.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" native-type="submit">登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="注册">
          <el-form ref="form" :model="formData" label-width="80px">
            <el-form-item label="用户ID">
              <el-input v-model="formData.id"></el-input>
            </el-form-item>
            <el-form-item label="用户名">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input type="password" v-model="formData.password"></el-input>
            </el-form-item>
            <el-form-item label="用户联系方式">
              <el-input v-model="formData.phone"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">注册</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>


<script>
import axiosInstance from '@/router/axiosInstance';

export default {
  data() {
    return {
      loginForm: {
        name: '',
        password: ''
      },
      formData: {
        id: '',
        name: '',
        phone: '',
        password: ''
      }
    };
  },
  methods: {
    handleLogin() {
      axiosInstance.post('http://localhost:8080/api/user/login', this.loginForm)
        .then(response => {
          if (response.data.code === 2000) {
            const token = response.data.data.token;
            localStorage.setItem('token', token);

            this.$message({
              message: '登录成功',
              type: 'success'
            });
            this.$router.push('/home');
          } else {
            this.$message({
              message: '用户名或密码错误',
              type: 'error'
            });
          }
        })
        .catch(error => {
          console.error('登录失败:', error);
          this.$message({
            message: '登录失败，请稍后再试',
            type: 'error'
          });
        });
    },
    submitForm() {
        //this.formData = { id: '', name: '', phone: '',password:''};
        // 新增操作
        axiosInstance.post('http://localhost:8080/api/user/add', this.formData)
          .then(response => {
            console.log('新增成功:', response.data);
            this.$message({
              message: '注册成功',
              type: 'success'
            });
            
          })
          .catch(error => {
            console.error('新增失败:', error);
            this.$message({
              message: '注册失败，请稍后再试',
              type: 'error'
            });
          });
      
    }
  }
};
</script>


<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('@/assets/R-C (1).jpg');
  background-size: cover;
  z-index: -1;
  opacity: 0.7; /* 调整背景透明度 */
}

.login-form-wrapper {
  max-width: 500px;
  width: 100%;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  background-color: rgba(255, 255, 255, 0.9); /* 更高的透明度 */
  text-align: center;
}

.login-form {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.el-form-item {
  margin-bottom: 16px;
}

.el-input__inner {
  height: 40px;
  line-height: 40px;
  font-size: 16px;
}

.el-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

.el-tabs__item {
  font-size: 18px;
  color: #333;
}

.el-tabs__header {
  margin-bottom: 20px;
}

.el-tabs__content {
  padding: 20px;
}

/* 优化注册表单的样式 */
.el-form {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.el-form-item__label {
  font-weight: bold;
  color: #333;
}

.el-form-item__content {
  margin-left: 0 !important; /* 重置默认的label宽度 */
}
</style>
